package com.wiloon.java8x.thread.CyclicBarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by wiloon on 6/1/17;
 */
public class People implements Runnable {
    private CyclicBarrier barrier;
    private int id;

    public People(int id, CyclicBarrier barrier) {
        this.id = id;
        this.barrier = barrier;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + ":People " + id + " is working.");
        long time = (long) (Math.random() * 2000);
        try {
            Thread.sleep(time);
            System.out.println("People " + id + " complete Task with " + time + "ms");
            barrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        System.out.println("All people complete work.People " + id + " begin other task.");
    }


}